package GUI;

import java.awt.EventQueue;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JComboBox;
import javax.swing.JButton;

import Controller.Client;
import Entity.*;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * this is the GUI for report creation. the parking lot manager can choose a report (complaints , order or disable parking spot) and the dates.  
 * @author Shahar Shelly
 *
 */
public class ReportCreation extends JFrame 
{
	JFrame frame;
	JPanel lastPanel;
	public void setLastPanel (JPanel panel)
	{
		this.lastPanel=panel;
	}
	public JPanel getLastPanel()
	{
		return (JPanel)lastPanel;
	}
	public void setFrame (JFrame frame)
	{
		this.frame=frame;
	}
	private JPanel contentPane;

	/**
	 * is the constructor. it has check box and combo box for choosing option and dates. it will also validate the date option.
	 */
	public ReportCreation() {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblCreateReport = new JLabel("Create Report");
		lblCreateReport.setBounds(159, 11, 120, 14);
		contentPane.add(lblCreateReport);
		
		final JRadioButton rdbtnOrdersReport = new JRadioButton("Orders Report");
		rdbtnOrdersReport.setBounds(6, 42, 109, 23);
		contentPane.add(rdbtnOrdersReport);
		
		final JRadioButton rdbtnComplaintsStatusReport = new JRadioButton("Complaints Status Report");
		rdbtnComplaintsStatusReport.setBounds(6, 93, 176, 23);
		contentPane.add(rdbtnComplaintsStatusReport);
		
		final JRadioButton rdbtnDisabledParkingSpots = new JRadioButton("Disabled Parking Spots Report");
		rdbtnDisabledParkingSpots.setBounds(6, 154, 202, 23);
		contentPane.add(rdbtnDisabledParkingSpots);
		
		final JComboBox<String> sDay1 = new JComboBox<String>();
		sDay1.setEnabled(false);
		sDay1.setBounds(228, 36, 38, 22);
		contentPane.add(sDay1);
		for(int i=1; i<=31; i++) sDay1.addItem(Integer.toString(i));
		
		final JComboBox<String> sMonth1 = new JComboBox<String>();
		sMonth1.setEnabled(false);
		sMonth1.setBounds(283, 37, 38, 20);
		contentPane.add(sMonth1);
		for(int i=1; i<=12; i++) sMonth1.addItem(Integer.toString(i));
		
		final JComboBox<String> sYear1 = new JComboBox<String>();
		sYear1.setEnabled(false);
		sYear1.setBounds(331, 37, 50, 20);
		contentPane.add(sYear1);
		for(int i=2000; i<=2999; i++) sYear1.addItem(Integer.toString(i));
		
		final JComboBox<String> eDay1 = new JComboBox<String>();
		eDay1.setEnabled(false);
		eDay1.setBounds(228, 63, 38, 22);
		contentPane.add(eDay1);
		for(int i=1; i<=31; i++) eDay1.addItem(Integer.toString(i));
		
		final JComboBox<String> eMonth1 = new JComboBox<String>();
		eMonth1.setEnabled(false);
		eMonth1.setBounds(283, 63, 38, 20);
		contentPane.add(eMonth1);
		for(int i=1; i<=12; i++) eMonth1.addItem(Integer.toString(i));
		
		final JComboBox<String> eYear1 = new JComboBox<String>();
		eYear1.setEnabled(false);
		eYear1.setBounds(331, 63, 50, 20);
		contentPane.add(eYear1);
		for(int i=2000; i<=2999; i++) eYear1.addItem(Integer.toString(i));
		
		final JComboBox<String> sDay2 = new JComboBox<String>();
		sDay2.setEnabled(false);
		sDay2.setBounds(228, 94, 38, 20);
		contentPane.add(sDay2);
		for(int i=1; i<=31; i++) sDay2.addItem(Integer.toString(i));
		
		final JComboBox<String> sMonth2 = new JComboBox<String>();
		sMonth2.setEnabled(false);
		sMonth2.setBounds(283, 94, 38, 20);
		contentPane.add(sMonth2);
		for(int i=1; i<=12; i++) sMonth2.addItem(Integer.toString(i));
		
		final JComboBox<String> sYear2 = new JComboBox<String>();
		sYear2.setEnabled(false);
		sYear2.setBounds(331, 94, 50, 20);
		contentPane.add(sYear2);
		for(int i=2000; i<=2999; i++) sYear2.addItem(Integer.toString(i));
		
		final JComboBox<String> eDay2 = new JComboBox<String>();
		eDay2.setEnabled(false);
		eDay2.setBounds(228, 122, 38, 22);
		contentPane.add(eDay2);
		for(int i=1; i<=31; i++) eDay2.addItem(Integer.toString(i));
		
		final JComboBox<String> eMonth2 = new JComboBox<String>();
		eMonth2.setEnabled(false);
		eMonth2.setBounds(283, 123, 38, 20);
		contentPane.add(eMonth2);
		for(int i=1; i<=12; i++) eMonth2.addItem(Integer.toString(i));
		
		final JComboBox<String> eYear2 = new JComboBox<String>();
		eYear2.setEnabled(false);
		eYear2.setBounds(331, 123, 50, 20);
		contentPane.add(eYear2);
		for(int i=2000; i<=2999; i++) eYear2.addItem(Integer.toString(i));
		
		final JComboBox<String> sDay3 = new JComboBox<String>();
		sDay3.setEnabled(false);
		sDay3.setBounds(228, 155, 38, 20);
		contentPane.add(sDay3);
		for(int i=1; i<=31; i++) sDay3.addItem(Integer.toString(i));
		
		final JComboBox<String> sMonth3 = new JComboBox<String>();
		sMonth3.setEnabled(false);
		sMonth3.setBounds(280, 156, 38, 20);
		contentPane.add(sMonth3);
		for(int i=1; i<=12; i++) sMonth3.addItem(Integer.toString(i));
		
		final JComboBox<String> sYear3 = new JComboBox<String>();
		sYear3.setEnabled(false);
		sYear3.setBounds(331, 156, 50, 20);
		contentPane.add(sYear3);
		for(int i=2000; i<=2999; i++) sYear3.addItem(Integer.toString(i));
		
		final JComboBox<String> eDay3 = new JComboBox<String>();
		eDay3.setEnabled(false);
		eDay3.setBounds(228, 186, 38, 20);
		contentPane.add(eDay3);
		for(int i=1; i<=31; i++) eDay3.addItem(Integer.toString(i));
		
		final JComboBox<String> eMonth3 = new JComboBox<String>();
		eMonth3.setEnabled(false);
		eMonth3.setBounds(280, 187, 38, 20);
		contentPane.add(eMonth3);
		for(int i=1; i<=12; i++) eMonth3.addItem(Integer.toString(i));
		
		final JComboBox<String> eYear3 = new JComboBox<String>();
		eYear3.setEnabled(false);
		eYear3.setBounds(331, 187, 50, 20);
		contentPane.add(eYear3);
		for(int i=2000; i<=2999; i++) eYear3.addItem(Integer.toString(i));
		
		JButton btnCreate = new JButton("Create");
		btnCreate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		btnCreate.setBounds(105, 228, 89, 23);
		contentPane.add(btnCreate);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				close();
				frame.setContentPane(lastPanel);
				frame.setVisible(true);
			}
		});
		btnCancel.setBounds(232, 228, 89, 23);
		contentPane.add(btnCancel);
		
		
		
		/////////////////////////////////Listeners
		rdbtnOrdersReport.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				rdbtnComplaintsStatusReport.setSelected(false);
				rdbtnDisabledParkingSpots.setSelected(false);
				sDay1.setEnabled(true);
				sDay2.setEnabled(false);
				sDay3.setEnabled(false);
				sMonth1.setEnabled(true);
				sMonth2.setEnabled(false);
				sMonth3.setEnabled(false);
				sYear1.setEnabled(true);
				sYear2.setEnabled(false);
				sYear3.setEnabled(false);
				
				eDay1.setEnabled(true);
				eDay2.setEnabled(false);
				eDay3.setEnabled(false);
				eMonth1.setEnabled(true);
				eMonth2.setEnabled(false);
				eMonth3.setEnabled(false);
				eYear1.setEnabled(true);
				eYear2.setEnabled(false);
				eYear3.setEnabled(false);
			}
		});
		
		rdbtnComplaintsStatusReport.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				rdbtnOrdersReport.setSelected(false);
				rdbtnDisabledParkingSpots.setSelected(false);
				sDay1.setEnabled(false);
				sDay2.setEnabled(true);
				sDay3.setEnabled(false);
				sMonth1.setEnabled(false);
				sMonth2.setEnabled(true);
				sMonth3.setEnabled(false);
				sYear1.setEnabled(false);
				sYear2.setEnabled(true);
				sYear3.setEnabled(false);
				
				eDay1.setEnabled(false);
				eDay2.setEnabled(true);
				eDay3.setEnabled(false);
				eMonth1.setEnabled(false);
				eMonth2.setEnabled(true);
				eMonth3.setEnabled(false);
				eYear1.setEnabled(false);
				eYear2.setEnabled(true);
				eYear3.setEnabled(false);
			}
		});
		
		rdbtnDisabledParkingSpots.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				rdbtnOrdersReport.setSelected(false);
				rdbtnComplaintsStatusReport.setSelected(false);
				sDay1.setEnabled(false);
				sDay2.setEnabled(false);
				sDay3.setEnabled(true);
				sMonth1.setEnabled(false);
				sMonth2.setEnabled(false);
				sMonth3.setEnabled(true);
				sYear1.setEnabled(false);
				sYear2.setEnabled(false);
				sYear3.setEnabled(true);
				
				eDay1.setEnabled(false);
				eDay2.setEnabled(false);
				eDay3.setEnabled(true);
				eMonth1.setEnabled(false);
				eMonth2.setEnabled(false);
				eMonth3.setEnabled(true);
				eYear1.setEnabled(false);
				eYear2.setEnabled(false);
				eYear3.setEnabled(true);
			}
		});
		
		btnCreate.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				Date st=new Date();
				Date en=new Date();
				if(rdbtnOrdersReport.isEnabled())
				{
					st.setDate(Integer.valueOf((String) sDay1.getSelectedItem()));
					st.setMonth(Integer.valueOf((String) sMonth1.getSelectedItem()));
					st.setYear(Integer.valueOf((String) sYear1.getSelectedItem()));
					en.setDate(Integer.valueOf((String) eDay1.getSelectedItem()));
					en.setMonth(Integer.valueOf((String) eMonth1.getSelectedItem()));
					en.setYear(Integer.valueOf((String) eYear1.getSelectedItem()));
					String s1=new String(st.getMonth()+"/"+st.getDate()+"/"+st.getYear());
					String s2=new String(en.getMonth()+"/"+en.getDate()+"/"+en.getYear());
					if(!ReportCreation.isValidDate(s1))
						JOptionPane.showMessageDialog(null,"Wrong start date","Report Creation",JOptionPane.ERROR_MESSAGE);	
					else if(!ReportCreation.isValidDate(s2))
							JOptionPane.showMessageDialog(null,"Wrong end date","Report Creation",JOptionPane.ERROR_MESSAGE);
					else{
						OrderReport r=new OrderReport(st,en,0); 
						OrderReport.save(r);
					}
				}
				else if(rdbtnComplaintsStatusReport.isEnabled())
				{
					st.setDate(Integer.valueOf((String) sDay2.getSelectedItem()));
					st.setMonth(Integer.valueOf((String) sMonth2.getSelectedItem()));
					st.setYear(Integer.valueOf((String) sYear2.getSelectedItem()));
					en.setDate(Integer.valueOf((String) eDay2.getSelectedItem()));
					en.setMonth(Integer.valueOf((String) eMonth2.getSelectedItem()));
					en.setYear(Integer.valueOf((String) eYear2.getSelectedItem()));
					String s1=new String(st.getMonth()+"/"+st.getDate()+"/"+st.getYear());
					String s2=new String(en.getMonth()+"/"+en.getDate()+"/"+en.getYear());
					if(!ReportCreation.isValidDate(s1))
						JOptionPane.showMessageDialog(null,"Wrong start date","Report Creation",JOptionPane.ERROR_MESSAGE);	
					else if(!ReportCreation.isValidDate(s2))
							JOptionPane.showMessageDialog(null,"Wrong end date","Report Creation",JOptionPane.ERROR_MESSAGE);
					else{
						ComplaintReport r=new ComplaintReport(st,en,0); 
						ComplaintReport.save(r);
					}
				}
				else if(rdbtnDisabledParkingSpots.isEnabled())
				{
					st.setDate(Integer.valueOf((String) sDay3.getSelectedItem()));
					st.setMonth(Integer.valueOf((String) sMonth3.getSelectedItem()));
					st.setYear(Integer.valueOf((String) sYear3.getSelectedItem()));
					en.setDate(Integer.valueOf((String) eDay3.getSelectedItem()));
					en.setMonth(Integer.valueOf((String) eMonth3.getSelectedItem()));
					en.setYear(Integer.valueOf((String) eYear3.getSelectedItem()));
					String s1=new String(st.getMonth()+"/"+st.getDate()+"/"+st.getYear());
					String s2=new String(en.getMonth()+"/"+en.getDate()+"/"+en.getYear());
					if(!ReportCreation.isValidDate(s1))
						JOptionPane.showMessageDialog(null,"Wrong start date","Report Creation",JOptionPane.ERROR_MESSAGE);	
					else if(!ReportCreation.isValidDate(s2))
							JOptionPane.showMessageDialog(null,"Wrong end date","Report Creation",JOptionPane.ERROR_MESSAGE);
					else{
						DisabledReport r=new DisabledReport(st,en,0); 
						DisabledReport.save(r);
					}
				}
			}
		});
	}
	public void close()
	{
		WindowEvent windowclose=new WindowEvent(this,WindowEvent.WINDOW_CLOSING);
		Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(windowclose);
	}
	private static boolean isValidDate(String input) {
        String formatString = "MM/dd/yyyy";

        try {
            SimpleDateFormat format = new SimpleDateFormat(formatString);
            format.setLenient(false);
            format.parse(input);
        } catch (ParseException e) {
            return false;
        } catch (IllegalArgumentException e) {
            return false;
        }

        return true;
    }
}
